package com.fanw.easyExport;

import com.fanw.easyExport.core.EasyExport;
import com.fanw.easyExport.core.config.ExportConfig;
import com.fanw.easyExport.core.config.FieldConfig;
import com.fanw.easyExport.core.data.defaultJdbcImpl.JdbcDataFetcher;
import com.fanw.easyExport.core.data.defaultJdbcImpl.JdbcDataSourceConfig;
import com.fanw.easyExport.core.model.ExportResult;


import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
//        // 1. 创建导出配置
//        ExportConfig config = new ExportConfig();
//        config.setTaskName("UserExport");
//        config.setOutputPath("d.user_record");
//        config.setDelimiter("^!");
//        config.setIncludeHeader(true);
//
//        // 2. 配置字段映射
//        List<FieldConfig> fields = Arrays.asList(
//                new FieldConfig("id", "用户ID"),
//                new FieldConfig("name", "姓名"),
//                new FieldConfig("email", "邮箱"),
//                new FieldConfig("age", "年龄", "未知") // 设置默认值
//        );
//        config.setFields(fields);
//
//        // 3. 创建数据获取器
//        DemoDataFetcher dataFetcher = new DemoDataFetcher();
//
//        // 4. 执行导出
//        EasyExport exporter = new EasyExport(config, dataFetcher);
//        ExportResult result = exporter.execute();
//
//        // 5. 输出结果
//        System.out.println("导出任务: " + result.getTaskName());
//        System.out.println("状态: " + (result.isSuccess() ? "成功" : "失败"));
//        System.out.println("文件路径: " + result.getOutputPath());
//        System.out.println("导出行数: " + result.getRowCount());
//        System.out.println("消息: " + result.getMessage());




        //jdbc
        // 1. 创建JDBC数据源配置
        JdbcDataSourceConfig jdbcConfig = new JdbcDataSourceConfig();
        jdbcConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        jdbcConfig.setUrl("jdbc:mysql://localhost:3306/test_db?useSSL=false");
        jdbcConfig.setUsername("root");
        jdbcConfig.setPassword("root");
        jdbcConfig.setQuery("SELECT id, username, email, created_at, status FROM users");

        // 2. 创建导出配置
        ExportConfig exportConfig = new ExportConfig();
        exportConfig.setTaskName("UserDatabaseExport");
        exportConfig.setOutputPath("users_from_db.csv");
        exportConfig.setDelimiter(",");
        exportConfig.setIncludeHeader(true);

        // 3. 配置字段映射
        List<FieldConfig> fields = Arrays.asList(
                new FieldConfig("id", "用户ID"),
                new FieldConfig("username", "用户名"),
                new FieldConfig("email", "电子邮箱"),
                new FieldConfig("created_at", "创建时间"),
                new FieldConfig("status", "状态", "UNKNOWN") // 设置默认值
        );
        exportConfig.setFields(fields);

        // 4. 创建JDBC数据获取器
        try (JdbcDataFetcher dataFetcher = new JdbcDataFetcher(jdbcConfig)) {
            // 5. 创建并执行导出
            EasyExport exporter = new EasyExport(exportConfig, dataFetcher);
            ExportResult result = exporter.execute();

            // 6. 输出结果
            System.out.println("导出任务: " + result.getTaskName());
            System.out.println("状态: " + (result.isSuccess() ? "成功" : "失败"));
            System.out.println("文件路径: " + result.getOutputPath());
            System.out.println("导出行数: " + result.getRowCount());
            System.out.println("消息: " + result.getMessage());
        }
    }
}
